package com.lanmaoly.cunzheng;

import java.text.DecimalFormat;

public class FileTest {

    public static void main(String[] args) {
        String netFileSizeDescription = getNetFileSizeDescription((1024L * 1024L * 512L));
        System.out.println(netFileSizeDescription);
        System.out.println(1000 * 1000 * 500);
    }

    public static String getNetFileSizeDescription(Long size) {
        if (size != null) {
            StringBuffer bytes = new StringBuffer();
            DecimalFormat format = new DecimalFormat("###");
            if (size >= 1024 * 1024 * 1024) {
                double i = (size / (1024.0 * 1024.0 * 1024.0));
                bytes.append(format.format(i)).append("GB");
            } else if (size >= 1024 * 1024) {
                double i = (size / (1024.0 * 1024.0));
                bytes.append(format.format(i)).append("MB");
            } else if (size >= 1024) {
                double i = (size / (1024.0));
                bytes.append(format.format(i)).append("KB");
            } else if (size < 1024) {
                if (size <= 0) {
                    bytes.append("0B");
                } else {
                    bytes.append(size.intValue()).append("B");
                }
            }
            return bytes.toString();
        } else {
            return null;
        }
    }

}
